package ws.jdbc.concurrent.driver.sqlserver.tds.datatypes

import ws.utils.io.LittleEndianDataInputStream
/**
 * Test of TypeInfoVarLenType class methods.
 *
 * @author Wilson Santos, 36228 <wilson.santos@ua.pt>
 */
class TypeInfoVarLenTypeTest extends GroovyTestCase {

  void testRead() {
    println "testRead()"

    def data = [
      0x03, 0x00, 0x09, 0x04, 0xD0, 0x00, 0x34
    ] as Byte[]

    sendData(data)
    def is = receiveData()
    def lis = new LittleEndianDataInputStream(is)

    def info = new TypeInfoVarLenType((byte) 0xA7)
    info.read(lis)

    assertEquals 3, info.typevarlen.length
    println info.collation
  }

  static String host = "localhost"
	static int port = 5454
  static ServerSocket serverSocket

  static {
    serverSocket = new ServerSocket(port)
  }

  void setUp() {
    println "-----------------------------------------------------------------"
  }

  /* ----------------------------- TEST UTILITIES --------------------------- */
  static void sendData(Byte[] data) {
    Thread.start {
      def ios = serverSocket.accept()
      def os = new DataOutputStream(new BufferedOutputStream(ios.getOutputStream()))
      os.write(data)
      os.flush()
    }
  }

  static InputStream receiveData() {
    def sock = new Socket(host, port)
    return new DataInputStream(new BufferedInputStream(sock.getInputStream()))
  }
}

